package com.cyt.reward.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.reward.dao.RewardTagDao;
import com.cyt.reward.model.po.RewardTag;
import com.cyt.reward.model.vo.RewardTagVo;
import com.cyt.reward.service.RewardTagService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 打赏功能-评价标签Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-24 14:02:15
 */
@Service("rewardTagService")
public class RewardTagServiceImpl extends BaseServiceImpl<RewardTag> implements RewardTagService {

    @Resource
    private RewardTagDao rewardTagDao;

    @Autowired
    public void setBaseDao(RewardTagDao rewardTagDao) {
        this.baseDao = rewardTagDao;
    }

    /**
     * 分页查询
     *
     * @param rewardTagVo 接受参数对象
     * @param pageNum     分页数
     * @param pageSize    每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:02:15
     */
    @Override
    public ReturnResult page(RewardTagVo rewardTagVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<RewardTagVo> list = rewardTagDao.selectByCondition(rewardTagVo);
        PageInfo<RewardTagVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param rewardTagVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:02:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(RewardTagVo rewardTagVo, SysUser sysUser) {
        if (StrUtil.isEmpty(rewardTagVo.getId())) {
            RewardTag rewardTag = new RewardTag(sysUser);
            rewardTag.setApplyId(rewardTagVo.getApplyId());
            rewardTag.setName(rewardTagVo.getName());
            rewardTag.setOrderNum(rewardTagVo.getOrderNum());
            rewardTag.setValidState(rewardTagVo.getValidState());
            int num = rewardTagDao.insert(rewardTag);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            RewardTag rewardTag = rewardTagDao.selectByPrimaryKey(rewardTagVo.getId());
            rewardTag.setApplyId(rewardTagVo.getApplyId());
            rewardTag.setName(rewardTagVo.getName());
            rewardTag.setOrderNum(rewardTagVo.getOrderNum());
            rewardTag.setValidState(rewardTagVo.getValidState());
            rewardTag.setUpdateTime(new Date());
            rewardTag.setUpdateUser(sysUser.getId());
            int num = rewardTagDao.updateByPrimaryKey(rewardTag);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param rewardTagVo 参数接受对象
     * @param sysUser     当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:02:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(RewardTagVo rewardTagVo, SysUser sysUser) {
        //要修改的字段
        RewardTag rewardTag = new RewardTag();
        rewardTag.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        rewardTag.setUpdateUser(sysUser.getId());
        rewardTag.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(RewardTag.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(rewardTagVo.getId())) {
            criteria.andIn("id", Arrays.asList(rewardTagVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        rewardTagDao.updateByExampleSelective(rewardTag, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param rewardTagVo 参数接受对象
     * @return List<RewardTagVo>
     * @author rong yuan
     * @date 2019/7/25 09:42
     */
    @Override
    public List<RewardTagVo> list(RewardTagVo rewardTagVo) {
        rewardTagVo.setValidState(DictCode.BASE_STATUS_NORMAL);
        return rewardTagDao.selectByCondition(rewardTagVo);
    }
}